package cn.bugDance.trigger.job;

import cn.bugDance.domain.strategy.model.valobj.StrategyDelayJobQueueData;
import cn.bugDance.domain.strategy.service.IStrategyQuotaService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class StrategyAwardQuotaSubtractionJob {

    @Resource
    private IStrategyQuotaService strategyQuotaService;

    @Resource
    private RedissonClient redissonClient;

    @Scheduled(cron = "0/5 * * * * ?")
    public void exec(){
       RLock lock = redissonClient.getLock("big-market-UpdateActivityAwardStockJob");
       boolean result = false;
        try{
            result = lock.tryLock(3, 3, TimeUnit.SECONDS);
            if(!result) return;
            StrategyDelayJobQueueData strategyDelayJobQueueData = strategyQuotaService.takeStrategyJobQueue();
            if(null == strategyDelayJobQueueData) return;
            log.info("【触发层--StrategyAwardQuotaSubtractionJob】: 获取到更新任务, strategyId: {}, awardId: {}", strategyDelayJobQueueData.getStrategyId(), strategyDelayJobQueueData.getAwardId());

            strategyQuotaService.strategyAwardSubtractQuota(strategyDelayJobQueueData);
        }catch (Exception e){
            log.warn("【触发层--StrategyAwardQuotaSubtractionJob】: 执行策略奖品库存更新任务失败");
        }
    }
}
